<?php
/**
 * sitechannel.php
 * Create on 2015-03-25
 * Create by yuebin
 */
namespace haibao\cms\data;

class SiteChannel extends \haibao\cms\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\cms\model\data\site\SiteChannel');
    }
    
	/**
	 * 获取站点频道信息
	 * @param : Int $channelId
	 */
    public function getSiteChannelById($channelId){
    
    	return $this->getOneById($channelId);
    }
    
    /**
     * 添加站点频道
     * @param : Object \haibao\cms\model\data\site\SiteChannel $siteChannelModel
     */
    public function addSiteChannel($siteChannelModel){
    	
    	$this->beginTranscation();
    	try{
    	
    		$this->add($siteChannelModel);
    		 
    	} catch(\Exception $e){
    		 
    		$this->rollback();
    		throw $e;
    		 
    	}
    	$this->endTranscation();
 
    }
    /**
     * 根据站点ID更新站点频道
     * @param : Object \haibao\cms\model\data\site\SiteChannel $siteChannelModel
     */
    public function updateSiteChanneleById($siteChannelModel){
    	$arrSet =
    	array(
    			\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_CHANNEL_NAME=>$siteChannelModel->ChannelName,
    			\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_CHANNEL_URL=>$siteChannelModel->ChannelUrl,
    	);
    	$this->beginTranscation();
    	try{
    
    		$this->updateById($siteChannelModel->Id, $arrSet);
    		$this->saveSiteChannelTag($siteChannelModel);
    	
    		 
    	} catch(\Exception $e){
    		 
    		$this->rollback();
    		throw $e;
    		 
    	}
    	$this->endTranscation();
    }
    
    /**
     * 判断当前频道是否存在子频道 （未删除）
     * @pamam : Int $channelId
     */
    public function isExistChildChannel($channelId){
    
    	$sql = "select count(*) as childNum from `SiteChannel` where ParentId=%s and status=%s";
    	$channelInfo = $this->query($sql,array($channelId,\haibao\cms\model\data\site\SiteChannel::STATUS_ENABLE))->fetch_assoc();
    	
    	return !empty($channelInfo['childNum']) ? true : false;
    }
    
    /**
     * 判断当前频道是否存在子频道
     * @pamam : Int $channelId
     */
    public function isExistParentChannel($parentId){
    
    	$sql = "select * from `SiteChannel` where Id=%s and status=%s";
    	$channelInfo = $this->query($sql,array($parentId,\haibao\cms\model\data\site\SiteChannel::STATUS_ENABLE))->fetch_assoc();
    	 
    	return !empty($channelInfo) ? true : false;
    }
    /**
     * 删除站点频道
     * @param : Int $channelId
     */
    public function deleteSiteChannelById($channelId,$status = \haibao\cms\model\data\site\SiteChannel::STATUS_DIS_ENABLE){
    	 
    	$this->updateById($channelId,
    		array(
    			\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_STATUS => $status
    		)
    	);
    }

    /**
     * 根据站点名字获取站点信息
     * @param : String $channelName
     * @param : Int $siteId
     */
    public function getSiteChannelByChannelName($channelName, $channelId = null){
    	$model = null;
    	$sql = "select * from `SiteChannel` where ChannelName=%s";
    	$valArr = array($channelName);
    	if($channelId){
    		$sql .= " and Id <>%s";
    		array_push($valArr,$channelId);
    	}
    	$result = $this->query($sql, $valArr);
    	while ($result->fetch_assoc()){
    		$model = new \haibao\cms\model\data\site\SiteDefine();
    		foreach($model->getKeyMap() as $keyName=>$keyType){
    			$model->$keyName = $result->getData($keyType->getFieldName());
    		}
    	}
    	return $model;
    }
    
    /**
     * 修改站点频道关联标签
     */
    private function saveSiteChannelTag($siteChannelModel){
    	
    	if($siteChannelModel->SiteChannelTag){
    		
    		$sql = 'delete from `SiteChannelTag` where ChannelId=%s';
    		$this->query($sql,array($siteChannelModel->Id));
    		
    		$this->addAll($siteChannelModel->SiteChannelTag);
    	}else{
    		$sql = 'delete from `SiteChannelTag` where ChannelId=%s';
    		$this->query($sql,array($siteChannelModel->Id));
    	}
    }
}
